package com.rxh.controller;

import com.rxh.pojo.Excel;
import com.rxh.pojo.base.SearchInfo;
import com.rxh.pojo.base.Page;
import com.rxh.pojo.base.PageResult;
import com.rxh.pojo.core.CoreMerchantOrder;
import com.rxh.pojo.core.CoreOrder;
import com.rxh.pojo.core.CoreOrderChange;
import com.rxh.pojo.sys.SysConstant;
import com.rxh.service.ConstantService;
import com.rxh.service.TransactionService;
import com.rxh.spring.annotation.SystemLogInfo;
import com.rxh.util.ExcelUtils;
import com.rxh.util.UserInfoUtils;
import com.rxh.utils.BatchOperationUtils;
import com.rxh.utils.SystemConstant;
import com.rxh.vo.VoCoreOrder;
import com.rxh.vo.VoCoreOrderExport;
import org.springframework.http.MediaType;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 陈俊雄
 * Date: 2018/3/1
 * Time: 16:50
 * Project: Management
 * Package: com.rxh.controller
 */
@Controller
@RequestMapping(value = "/trans")
public class TransactionController {

    @Resource
    private TransactionService transactionService;
    @Resource
    private ConstantService constantService;

    /**
     * 获取交易信息
     *
     * @param page 分页对象
     * @return 查询结果
     */
    @SystemLogInfo(description = "订单查询")
    @RequestMapping(value = "/getTransInfo")
    @ResponseBody
    public PageResult getItemList(@RequestBody Page page) {
        return transactionService.getOrderList(page);
    }

    /**
     * 获取订单统计信息
     *
     * @param searchInfo 查询信息
     * @return 统计结果
     */
    @RequestMapping(value = "/getStatistics")
    @ResponseBody
    public Map<String, Object> getMerchantTransStatistics(@RequestBody SearchInfo searchInfo) {
        return transactionService.getTransStatistics(searchInfo);
    }

    /**
     * 获取初始化信息
     *
     * @return 初始化信息
     */
    @RequestMapping(value = "/getInquireInit")
    @ResponseBody
    public Map<String, Object> getInquireInit() {
        return transactionService.getInquireInit();
    }

    /**
     * 获取订单详情
     *
     * @param order 订单对象
     * @return 订单详情
     */
    @SystemLogInfo(description = "获取订单详情")
    @RequestMapping(value = "/getDetail")
    @ResponseBody
    public Map<String, Object> getDetail(@RequestBody CoreOrder order) {
        return transactionService.getOrderDetail(order);
    }

    /**
     * 导出excel信息
     *
     * @param searchInfo 查询信息
     */
    @SystemLogInfo(description = "导出订单信息")
    @RequestMapping(value = "/downloadOrderInfo", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void downloadOrderInfo(HttpServletResponse response, @RequestBody(required = false) SearchInfo searchInfo) throws IOException {
        List<VoCoreOrderExport> orderExports = transactionService.getExportOrderInfo(searchInfo);
        String[] groupNames = {SystemConstant.BANK_STATUS, SystemConstant.EXCEPTION_STATUS};
        Map<String, Map<String, SysConstant>> constants = constantService.getConstantsMapByGroupNames(groupNames);
        ExcelUtils.exportOrderInfoToExcel(response, orderExports, constants);
    }

    /**
     * 批量操作
     *
     * @param multipartFiles 上传文件
     * @return 批量操作结果
     */
    @SystemLogInfo(description = "订单批量操作")
    @RequestMapping(value = "/batchOperation")
    @ResponseBody
    public List<CoreOrderChange> batchOperation(@RequestParam("file") MultipartFile multipartFiles) throws IOException {
        Excel excel = ExcelUtils.getWorkbooks(multipartFiles);
        if (excel != null) {
            return transactionService.batchOperation(BatchOperationUtils
                    .getOperation(excel, SecurityContextHolder.getContext().getAuthentication().getName()), excel.getType());
        }
        return null;
    }

    /**
     * 获取商户订单信息
     *
     * @param orderId 订单号
     * @return 商户订单信息
     */
    @SystemLogInfo(description = "获取商户订单信息")
    @RequestMapping(value = "/getMerchantOrder")
    @ResponseBody
    public CoreMerchantOrder orderChange(@RequestBody Long orderId) {
        return transactionService.getMerchantOrderByOrderId(orderId);
    }

    /**
     * 订单变更
     *
     * @param orderChange 变更对象
     * @return 变更结果
     */
    @SystemLogInfo(description = "订单变更")
    @RequestMapping(value = "/orderChange")
    @ResponseBody
    public boolean orderChange(@RequestBody CoreOrderChange orderChange) {
        orderChange.setApplicant(UserInfoUtils.getName());
        return transactionService.orderChange(orderChange);
    }

    /**
     * 是否需要审核
     *
     * @param searchInfo 只含订单ID
     * @return 是否未审核
     */
    @RequestMapping(value = "/unaudited")
    @ResponseBody
    public boolean unaudited(@RequestBody SearchInfo searchInfo) {
        searchInfo.setIdSearchType(SearchInfo.ID_SEARCH_TYPE_ORDER);
        return transactionService.getOrderChangeCountBySearchInfo(searchInfo) != 0;
    }

    /**
     * 获取拒付初始化信息
     *
     * @return 初始化信息
     */
    @RequestMapping(value = "/getRefusedInquireInit")
    @ResponseBody
    public Map<String, Object> getRefuseInquireInit() {
        return transactionService.getRefusedInquireInit();
    }

    @SystemLogInfo(description = "获取拒付统计列表")
    @RequestMapping(value = "/getRefusedStatistics")
    @ResponseBody
    public List<Map<String, Object>> getRefusedStatistics(@RequestBody SearchInfo searchInfo) {
        return transactionService.getRefusedStatistics(searchInfo);
    }



    @SystemLogInfo(description = "保证金查询")
    @RequestMapping(value = "/getBondInfo")
    @ResponseBody
    public PageResult getBondInfo(@RequestBody Page page) {
        return transactionService.getBondInfo(page);
    }

    @RequestMapping(value = "getBondInit")
    @ResponseBody
    public Map<String, Object> getBondInit(){
        return transactionService.getBondInit();
    }

    @SystemLogInfo(description = "导出保证金信息")
    @RequestMapping(value = "/downloadBondInfo")
    public void downloadBondInfo(HttpServletResponse response, @RequestBody(required = false) SearchInfo searchInfo) throws IOException {
        List<VoCoreOrder> orders = transactionService.getExportBondInfo(searchInfo);
        Map<String, SysConstant> bondStatus = constantService.getConstantsMapByGroupName(SystemConstant.BOND_STATUS);
        ExcelUtils.exportBondInfoToExcel(response, orders, bondStatus);
    }

    /**
     * 
     * 商户订单追踪初始化
     * @author xuzm
     * @date 20180510
     * 
     */
   
    @RequestMapping(value = "/getOrderTrackInit")
    @ResponseBody
    public Map<String,Object> getOrderTrackInit() {
    	
    	return transactionService.getOrderTrackInit();
       
    }
    
   /**
    *  
    * @param paramMap
    * @return
    * @author xuzm
    * @date 20180510
    * 
    */
    @RequestMapping(value = "/getOrderTrackInfo")
    @ResponseBody
    public Map<String,Object> getOrderTrackInfo(@RequestBody Map<String, Object> paramMap) {
    	
    	return transactionService.getOrderTrackInfo(paramMap);
       
    }
    
    
}